#pragma once

#include <string>
#include <utility>
#include <vector>

namespace solution_032
{

class Solution
{
public:
    int longestValidParentheses(std::string s)
    {
        int max_len = 0;
        std::vector<int> id_stack{-1};
        for (int i = 0; i < (int)s.size(); ++i) {
            if (s.at(i) == '(') {
                id_stack.push_back(i);
            } else {
                id_stack.pop_back();
                if (id_stack.empty())
                    id_stack.push_back(i);
                else
                    max_len = std::max(max_len, i - id_stack.back());
            }
        }
        return max_len;
    }
};

}  /* namespace solution_032 */
